package com.example.hyg_veintype.view.guideDialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.view.guideDialog.data.GuideBean;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: yyw
 * time: 2020/12/15 13:11
 * desc:
 */
public class GuideDialog extends Dialog implements View.OnClickListener {

    ViewPager2 vpGuide;
    ImageView imgCircle1;
    ImageView imgCircle2;
    ImageView imgCircle3;
    Button btKnow;
    private Context context;
    GuideVpAdapter guideVpAdapter;
    List<GuideBean> list = new ArrayList<>();


    public GuideDialog(Context context) {
        this(context, R.style.commonDialog);
        this.context = context;
    }

    public GuideDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.veintype_dialog_guide, null);
        setContentView(view);
        initView(view);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels); // 高度设置为屏幕
        lp.height = (int) (d.heightPixels);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //去掉背景色
        dialogWindow.setAttributes(lp);

        initData();
        guideVpAdapter = new GuideVpAdapter(list,context);
        vpGuide.setAdapter(guideVpAdapter);
        vpGuide.setOffscreenPageLimit(3);
        vpGuide.setPageTransformer(new ViewPager2.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                //position为将要呈现在屏幕的页面，-的是上一个，+的是下一个
                Float absPos=Math.abs(position);
                Float scaleX=1-absPos;
                Float scaleY=1-absPos;
                page.setScaleX(scaleX);
                page.setScaleY(scaleY);
                page.setAlpha(scaleX);

            }
        });
        vpGuide.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                imgCircle1.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_gray));
                imgCircle2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_gray));
                imgCircle3.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_gray));
                if (position == 0) {
                    imgCircle1.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_purple));
                } else if (position == 1) {
                    imgCircle2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_purple));
                } else if (position == 2) {
                    imgCircle3.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.veintype_circle_purple));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void initView(View view) {
        vpGuide=view.findViewById(R.id.vp_guide);
        imgCircle1=view.findViewById(R.id.img_circle1);
        imgCircle2=view.findViewById(R.id.img_circle2);
        imgCircle3=view.findViewById(R.id.img_circle3);
        btKnow=view.findViewById(R.id.bt_know);
        btKnow.setOnClickListener(this);
    }

    private void initData() {
        list.add(new GuideBean("采脉部位与心脏在同一水平线上", R.drawable.veintype_help1, R.drawable.veintype_help2));
        list.add(new GuideBean("找到左右手寸、关、尺位,找到搏动点", R.drawable.veintype_help3, R.drawable.veintype_help4));
        list.add(new GuideBean("佩戴腕带、诊断设备,开始检测", R.drawable.veintype_help5, R.drawable.veintype_help6));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_know) {
            dismiss();
        }
    }
}
